package net.pimpas.web.tests.context;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import net.pimpas.core.session.FrameworkSession;


public class FacesSession implements FrameworkSession {

	public Object getAttribute(String key) {
		if(FacesContext.getCurrentInstance() == null) return null;
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		return request.getSession(true).getAttribute(key);
	}

	public Object setAttribute(String key, Object value) {
		if(FacesContext.getCurrentInstance() == null) return null;
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		request.getSession(true).setAttribute(key, value);
		
		return value;
	}

	public Object removeAttribute(String key) {
		if(FacesContext.getCurrentInstance() == null) return null;
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		Object ob = this.getAttribute(key);
		if(ob == null) return null;
		request.getSession(true).removeAttribute(key);
		
		return ob;
	}
}